package com.summer.Thread.threadSafe.Runnable;

public class SellTicketWindow implements Runnable {
    private static int ticket = 100;
//    @Override
//    public void run() {
//        while (true){
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            synchronized (this){
//                if (ticket <= 0) break;
//                else {
//                    System.out.println(Thread.currentThread().getName()+"---出票成功---票号为:"+ticket);
//                    ticket--;
//                }
//        }
//    }
//
//    }
@Override
public void run() {
    while (ticket>0){
        sell();
    }

}
public static synchronized void sell(){
    if (ticket>0){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"---出票成功---票号为:"+ticket);
        ticket--;
    }

}
    public static void main(String[] args) {
        SellTicketWindow win = new SellTicketWindow();
        Thread thread1 = new Thread(win,"窗口1");
        Thread thread2 = new Thread(win,"窗口2");
        Thread thread3 = new Thread(win,"窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
